<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class AdminController extends AdminbaseController {

	function _initialize() {
		parent::_initialize ();
		$this->db = D ( "Admin" );
		$this->group = D ( "AuthGroup" );
		$this->group_access = D ( "AuthGroupAccess" );
	}
	function index() {
		
		$group_id=I('get.group_id');
		
		if($group_id){
			
		$count=M("AuthGroupAccess")->where(array("group_id"=>$group_id))->count();
			$page = $this->page($count);
			$users = M("AuthGroupAccess")->limit($page->firstRow . ',' . $page->listRows)->where(array("group_id"=>$group_id))->select ();
			
			foreach ($users as $key=>$v){
					
				$v[group]= $this->db->get_group_name($v[uid]);
				$result[]=$this->db->where(array("uid"=>$v['uid']))->find();
			}
			
			
		}
		else{
		
		if(I("get.keyword")){
		
			
			$where['nickname']  = array('like','%'.I("get.keyword").'%');
			
			$where['name']  = array('like','%'.I("get.keyword").'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		
		}

		$this->assign("keyword",I("get.keyword"));
		$count=$this->db->where($map)->count();
		$page = $this->page($count);
		$result=$this->db->limit($page->firstRow . ',' . $page->listRows)->order("uid asc")->where($map)->select();
		
		foreach ($result as $key=>$v){
		
			$result[$key][group]= $this->db->get_group_name($v[uid]);
		
		}
		
		
	
		
			}
	
	
		
		
		
	
	
			$this->assign("Page",$page->show(""));
		$this->assign ( "users", $result );
		$this->display ();
	}

	public function log(){

		if(I("get.keyword")){




			$map['name']  = array('like','%'.I("get.keyword").'%');

		}

		$count=D("AdminLog")->where($map)->count();
		$page = $this->page($count);
		$result=D("AdminLog")->limit($page->firstRow . ',' . $page->listRows)->order("inputtime desc")->where($map)->select();
		$this->assign ( "_list", $result );


		$this->display ();





	}
	function add() {
		if (IS_POST) {
			
			if ($this->db->create ()) {
				if ($uid=$this->db->add ()) {
					
				foreach ($_POST['group_id'] as $key=>$v){
					$d=array(
						"uid"=>$uid,
							"group_id"=>$v,
							
					);
					$this->group_access->add($d);
					
				}
					
					
					$this->success ( "添加成功！", U ( "Admin/Admin/index" ) );
				} else {
					$this->error ( "添加失败！" );
				}
			} else {
				$this->error ( $this->db->getError () );
			}
		} else {
			
			
			$groups = $this->group->where ( "status=1" )->select ();
				
			foreach ($groups as $key=>$v){
			
				$s=$this->group_access->where(array("uid"=>$uid,"group_id"=>$v['id']))->count();
					
				if($s) $groups[$key][checked]= $s? ' checked' : '';
			}
				
				
			$this->assign ( "groups", $groups );
			
			
			$this->display ();
		}
	}

	function edit() {
		if (IS_POST) {
			
			
			if ( $_POST ['pass']=='') {
				unset ( $_POST ['pass'] );
			}else{
				$_POST ['pass']=lap_password($_POST ['pass']);
				
			}
			if ($this->db->create ()) {
				$result = $this->db->save ();
				if ($result !== false) {
					
					$this->group_access->where(array("uid"=>$_POST ['uid']))->delete();
					
					foreach ($_POST['group_id'] as $key=>$v){
						$d=array(
								"uid"=>$_POST ['uid'],
								"group_id"=>$v,
									
						);
						$this->group_access->add($d);
							
					}
					
					
					$this->success ( "保存成功！" , U ( "Admin/Admin/index" ) );
				} else {
					$this->error ( "保存失败！" );
				}
			} else {
				$this->error ( $this->db->getError () );
			}
		} else {	
			
			$uid = intval ( I ( "get.uid" ) );
			$groups = $this->group->where ( "status=1" )->select ();
			
			foreach ($groups as $key=>$v){
				
				$s=$this->group_access->where(array("uid"=>$uid,"group_id"=>$v['id']))->count();
			
				if($s) $groups[$key][checked]= $s? ' checked' : '';
			}
			
			
			$this->assign ( "groups", $groups );
			
			$user = $this->db->where ( array (
					"uid" => $uid 
			) )->find ();
			$this->assign ( $user );
			
			$this->display ();
		}
	}
	
	/**
	 * 删除
	 */
	function delete() {
		$uid = intval ( I ( "get.uid" ) );
		if ($uid == 1) {
			$this->error ( "最高管理员不能删除！" );
		}
		
		if ($this->db->where ( "uid=$uid" )->delete () !== false) {
			D("AuthGroupAccess")->where ( "uid=$uid" )->delete();
			
			$this->success ( "删除成功！" );
			
			
		} else {
			$this->error ( "删除失败！" );
		}
	}
// 	function userinfo() {
// 		if (IS_POST) {
// 			$_POST [uid] = UID;
// 			if ($this->db->create ()) {
// 				if ($this->db->save () !== false) {
// 					$this->success ( "保存成功！" );
// 				} else {
// 					$this->error ( "保存失败！" );
// 				}
// 			} else {
// 				$this->error ( $this->db->getError () );
// 			}
// 		} else {
// 			$id = UID;
// 			$user = $this->db->where ( array (
// 					"uid" => $id 
// 			) )->find ();
// 			$this->assign ( $user );
// 			$this->display ();
// 		}
// 	}
	function password() {
		if (IS_POST) {
			if (empty ( $_POST ['old_password'] )) {
				$this->error ( "原始密码不能为空！" );
			}
			if (empty ( $_POST ['password'] )) {
				$this->error ( "新密码不能为空！" );
			}
			
			$uid = UID;
			$admin = $this->db->where ( array (
					"uid" => $uid 
			) )->find ();
			$old_password = $_POST ['old_password'];
			$password = $_POST ['password'];
			if (lap_password ( $old_password ) == $admin ['pass']) {
				if ($_POST ['password'] == $_POST ['repassword']) {
					if ($admin ['pass'] == lap_password ( $password )) {
						$this->error ( "新密码不能和原始密码相同！" );
					} else {
						$data ['pass'] = lap_password ( $password );
						$data ['uid'] = $uid;
						$r = $this->db->save ( $data );
						if ($r !== false) {
							$this->success ( "修改成功！" );
						} else {
							$this->error ( "修改失败！" );
						}
					}
				} else {
					$this->error ( "密码输入不一致！" );
				}
			} else {
				$this->error ( "原始密码不正确！" );
			}
		} else {
			$this->display ();
		}
	}

	
	
}